﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TKFA.Common;
using System.Data;

namespace FixedAssets.Controls
{
    public partial class CreateCheck_Control : System.Web.UI.UserControl
    {
       public static DataTable checkdt;
       public static DataTable viewdt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadALL();
                ddlistSCORPCODE_SelectedIndexChanged(sender, e);
                if (checkdt == null)
                {
                    checkdt = ((DataTable)this.C1GridView_Card.DataSource).Clone();
                    checkdt.Columns.Add("SDEVICECLASSCODE", typeof(string));
                    checkdt.PrimaryKey = new DataColumn[] { checkdt.Columns["SASSETSCODE"] };
                }
                C1.Web.Wijmo.Controls.C1GridView.C1GridViewRow gr = this.C1GridView_Card.SelectedRow;
            }
            else
            {
                SetCheckMasterData();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.ddlistSCORPCODE.SelectedValue.Trim().Length == 0)
            {
                strErr += "请选择单位！\\n";
            }
            if (this.txtSCHECKPLANNAME.Text.Trim().Length == 0)
            {
                strErr += "盘点名称不能为空！\\n";
            }
            //if (this.txtSCONTENTSCODE.Text.Trim().Length == 0)
            //{
            //    strErr += "SCONTENTSCODE不能为空！\\n";
            //}
            //if (this.txtSCP_MCARDCODE.Text.Trim().Length == 0)
            //{
            //    strErr += "SCP_MCARDCODE不能为空！\\n";
            //}
            if (this.ddlistSCP_ASSETSCODE.SelectedValue.Trim().Length == 0)
            {
                strErr += "盘点类别不能为空！\\n";
            }
            if (this.txtSCP_USER.Text.Trim().Length == 0)
            {
                strErr += "盘点人员不能为空！\\n";
            }
            if (this.txtSCP_STARTTIME.Text.Trim().Length == 0)
            {
                strErr += "盘点开始日期不能为空！\\n";
            }
            if (this.txtSCP_ENDTIME.Text.Trim().Length == 0)
            {
                strErr += "盘点结束日期不能为空！\\n";
            }
            if (this.txtSCP_MAKER.Text.Trim().Length == 0)
            {
                strErr += "SCP_MAKER不能为空！\\n";
            }

            if (strErr != "")
            {
                TKFA.Common.MessageBox.Show(this.Page, strErr);
                return;
            }
            string SCORPCODE = this.ddlistSCORPCODE.SelectedValue;
            string SCHECKPLANID = System.Guid.NewGuid().ToString();
            string SCHECKPLANNAME = this.txtSCHECKPLANNAME.Text;
            string SCP_USER = this.txtSCP_USER.Text;
            string SCP_STARTTIME = this.txtSCP_STARTTIME.Text;
            string SCP_ENDTIME = this.txtSCP_ENDTIME.Text;
            string SCP_MAKER = this.txtSCP_MAKER.Text;
            string SPERIOD = SCP_STARTTIME.Substring(0, 4);

            TKFA.BLL.FA_CHECKPLAN bll = new TKFA.BLL.FA_CHECKPLAN();
            foreach (DataRow dr in checkdt.Rows)
            {
                TKFA.Model.FA_CHECKPLAN model = new TKFA.Model.FA_CHECKPLAN();
                model.SCORPCODE = SCORPCODE;
                model.SCHECKPLANID = SCHECKPLANID;
                model.SPERIOD = SPERIOD;
                model.SCHECKPLANNAME = SCHECKPLANNAME;
                model.SCONTENTSCODE = dr["sassetscode"].ToString();
                model.SCP_MCARDCODE = dr["smastercardcode"].ToString();
                model.SCP_ASSETSCODE = dr["SDEVICECLASSCODE"].ToString();
                model.SCP_USER = SCP_USER;
                model.SCP_STARTTIME = SCP_STARTTIME;
                model.SCP_ENDTIME = SCP_ENDTIME;
                model.SCP_MAKER = SCP_MAKER;
                bll.Add(model);
            }
            TKFA.Common.MessageBox.Show(this.Page, "保存成功!");
        }

        private void LoadALL()
        {
            ddlistSCORPCODE.Items.Clear();
            TKFA.BLL.FA_CORP corp_bll = new TKFA.BLL.FA_CORP();
            //绑定单位
            List<TKFA.Model.FA_CORP> lcorp_model = corp_bll.GetModelList("");
            foreach (TKFA.Model.FA_CORP cm in lcorp_model)
            {
                ListItem li = new ListItem(cm.SCORPNAME, cm.SCORPCODE); ;
                ddlistSCORPCODE.Items.Add(li);
            }
            if (lcorp_model.Count > 0)
                ddlistSCP_ASSETSCODE.SelectedIndex = 0;
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {

        }

        protected void ddlistSCORPCODE_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.ddlistSCORPCODE.SelectedValue))
            {
                TKFA.Common.MessageBox.Show(this.Page, "请选择单位!");
                return;
            }
            TKFA.BLL.FA_DEVICECLASS_PROPERTY device_bll = new TKFA.BLL.FA_DEVICECLASS_PROPERTY();
            List<TKFA.Model.FA_DEVICECLASS_PROPERTY> ldevice_model = device_bll.GetModelList("scorpcode='" + this.ddlistSCORPCODE.SelectedValue + "'");
            ddlistSCP_ASSETSCODE.Items.Clear();
            foreach (TKFA.Model.FA_DEVICECLASS_PROPERTY dm in ldevice_model)
            {
                ListItem li = new ListItem(dm.SDEVICECLASS_NAME, dm.SDEVICECLASS_CODE); ;
                ddlistSCP_ASSETSCODE.Items.Add(li);
            }
            C1GridViewDataBind(this.C1GridView_Card, GetMasterData(this.ddlistSCORPCODE.SelectedValue, this.ddlistSCP_ASSETSCODE.SelectedValue));
            if(checkdt!=null)
            checkdt.Rows.Clear();
        }

        private void C1GridViewDataBind(C1.Web.Wijmo.Controls.C1GridView.C1GridView c1gridview, DataTable dt)
        {
            c1gridview.DataSource = dt;
            c1gridview.DataBind();
            object obj = c1gridview.DataKeys;
        }

        private DataTable GetMasterData(string scorpcode, string sdevicecode)
        {
            viewdt = new DataTable();
            TKFA.BLL.FA_MASTERDATA master_bll = new TKFA.BLL.FA_MASTERDATA();
            //绑定单位
            DataSet ds = master_bll.GetList(" scorpcode='" + scorpcode +
                         "' and SDEVICECLASS_CODE='" + sdevicecode + "'");
            if (ds.Tables.Count > 0)
            {
                viewdt = ds.Tables[0].DefaultView.ToTable(true, new string[] { "SMASTERCARDCODE", "SASSETSNAME", "SASSETSCODE", "SDEPARTMENTNAME", "SBUSE_DATE", "SKEEPER" });
                viewdt.PrimaryKey = new DataColumn[] { viewdt.Columns["SASSETSCODE"] };
            }
            return viewdt;
        }

        private void SetCheckMasterData()
        {
            foreach (string s in GetSelIDlist().Split(','))
            {
                if (!string.IsNullOrEmpty(s))
                {
                    DataRow drs = viewdt.Rows.Find(s);
                    if (drs!=null&& !checkdt.Rows.Contains(drs[viewdt.PrimaryKey[0].ColumnName]))
                    {
                        DataRow ndrs = checkdt.NewRow();

                        foreach (DataColumn dc in checkdt.Columns)
                        {
                            if (dc.ColumnName.Equals("SDEVICECLASSCODE"))
                            {
                                ndrs[dc.ColumnName] = this.ddlistSCP_ASSETSCODE.SelectedValue;
                            }
                            else
                            {
                                ndrs[dc.ColumnName] = drs[dc.ColumnName];
                            }
                        }

                        checkdt.Rows.Add(ndrs);
                    }
                }
            }
            C1GridViewDataBind(this.C1GridView2,checkdt);

        }

        protected void ddlistSCP_ASSETSCODE_SelectedIndexChanged(object sender, EventArgs e)
        {
            C1GridViewDataBind(this.C1GridView_Card, GetMasterData(this.ddlistSCORPCODE.SelectedValue, this.ddlistSCP_ASSETSCODE.SelectedValue));
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < this.C1GridView_Card.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)this.C1GridView_Card.Rows[i].FindControl("SelectCB");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    if (this.C1GridView_Card.DataKeyNames.Length > 0)
                    {
                        object DataKey = viewdt.Rows[i][this.C1GridView_Card.DataKeyNames[0]];
                        idlist += DataKey.ToString() + ",";
                        for (int j = 0; j < this.C1GridView2.Rows.Count; j++)
                        {
                            CheckBox ChkBxItem2 = (CheckBox)this.C1GridView2.Rows[j].FindControl("SelectCB");
                            if (ChkBxItem2 != null && (!ChkBxItem2.Checked)&&(this.C1GridView2.DataKeys[j].Value != null))
                            {
                                if (this.C1GridView2.DataKeys[j].Value.Equals(DataKey.ToString()))
                                {
                                    ((CheckBox)C1GridView_Card.Rows[i].FindControl("SelectCB")).Checked = false;
                                    idlist = idlist.Replace(DataKey.ToString() + ",", "");
                                    DataRow dr = checkdt.Rows.Find(C1GridView2.DataKeys[j].Value);
                                }
                                else
                                {
                                    checkdt.Rows.Remove(checkdt.Rows.Find(C1GridView2.DataKeys[j].Value));
                                }
                            }
                        }
                       
                    }
                }
            }
           
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        protected void C1Tabs1_SelectedChanged(object sender, EventArgs e)
        {
            SetCheckMasterData();
        }

    }
}